package abaco.gui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import abaco.Symbol;
import abaco.symbol.operation.Root;
import abaco.symbol.operation.arithmetic.Division;

/**
 *
 * @author Bruno Ferreira <brunosfweb@gmail.com>
 * @author Renan Barbosa  <renandelima@gmail.com>
 *
 */
public class Main extends JApplet implements ActionListener {
	private JMenu  menu;
	private Container content;
	private Workspace workspace;

	public Main() throws HeadlessException {}
	/**
	 * initialize the components for the Applet
	 */
	@Override
	public void init() {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				//inicializa os componentes graficos da applet
				initComponents();
			}
		});
	}

	/**
	 * initializes graphicals components
	 */
	private void initComponents() {
		//call Jpanel(workspace)
		createWorkspace();
		//Initializes main menu
		initializeMenu();
		//configure Applet content
		content = this.getContentPane();
		content.setLayout(new FlowLayout());
		content.add(workspace);
		Dimension d = new Dimension(400, 400);
		setSize(d);
		setMinimumSize(d);
		setPreferredSize(d);
	}

	private void createWorkspace() {
		workspace = new Workspace();
		Dimension d = new Dimension(400, 400);
		workspace.setMinimumSize(d);
		workspace.setPreferredSize(d);
		Manager.getInstance().setCurrentFocused(workspace);
	}

	private void initializeMenu() {
		JMenuBar   menuBar = new JMenuBar();
		menu = new JMenu("Symbols");
		JMenuItem constants = new JMenuItem("Root");
		constants.setIcon(new ImageIcon(getClass().getResource("/images/root.png")));
		menu.add (constants);
		constants.addActionListener(this);
		JMenuItem functions = new JMenuItem ("Functions");
		menu.add(functions);
		functions.addActionListener(this);
		JMenuItem division = new JMenuItem ("Division");
		menu.add(division);
		division.addActionListener(this);
		menuBar.add(menu);
		setJMenuBar(menuBar);
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub
		super.start();
	}

	@Override
	public void stop() {
		//stop the Applet
		super.stop();
		// close the Frame
		close();
	}

	/**
	 * Close the frame
	 */
	private void close() {
		// close
		workspace = null;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		Symbol newSymbol = null;
		String command = event.getActionCommand();
		if (command.equals("Root")) {
			newSymbol = new Root();
		} else if (command.equals("Division")) {
			newSymbol = new Division();
		}
		if (newSymbol.equals(null)) {
			return;
		}

		Manager manager = Manager.getInstance();
		Object focused = manager.getCurrentFocused();

		// if a editor panel is focused, then change focus to related symbol
		// it allow enhanced replacing
		if (focused instanceof Panel) {
			focused = ((Panel) focused).getSymbol();
		}

		if (focused instanceof Symbol) {
			// selected item is a symbol, let drawer do its job
			Drawer drawer = new Drawer((Symbol) focused);
			drawer.replace(newSymbol);
		} else if (focused instanceof JPanel) {
			// insert symbol in a panel
			Drawer drawer = new Drawer(newSymbol);
			drawer.draw((JPanel) focused);
		}
	}
}
